package com.outdoor.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.outdoor.model.dto.request.ApplicationRequest;
import com.outdoor.model.dto.response.ApiResponse;
import com.outdoor.model.entity.Application;
import com.outdoor.model.enums.ApplicationStatus;
import com.outdoor.service.ApplicationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 申请控制器
 * 
 * @author OutdoorTeam
 * @date 2025-05-18
 */
@Slf4j
@RestController
@RequestMapping("/api/applications")
@RequiredArgsConstructor
@Tag(name = "申请管理", description = "团队申请相关接口")
public class ApplicationController {

    private final ApplicationService applicationService;

    @PostMapping
    @Operation(summary = "创建申请", description = "申请加入团队")
    public ApiResponse<Application> createApplication(@Valid @RequestBody ApplicationRequest request) {
        // 模拟获取当前用户ID
        Long currentUserId = 4L;
        log.debug("创建申请请求: 团队ID={}, 用户ID={}", request.getTeamId(), currentUserId);
        
        Application application = applicationService.createApplication(request, currentUserId);
        return ApiResponse.success("申请提交成功", application);
    }

    @PutMapping("/{id}/process")
    @Operation(summary = "处理申请", description = "批准或拒绝申请")
    public ApiResponse<Application> processApplication(
            @PathVariable Long id,
            @RequestParam ApplicationStatus status) {
        
        // 模拟获取当前用户ID
        Long currentUserId = 1L;
        log.debug("处理申请请求: ID={}, 状态={}", id, status);
        
        Application application = applicationService.processApplication(id, status, currentUserId);
        String message = status == ApplicationStatus.APPROVED ? "申请已批准" : "申请已拒绝";
        return ApiResponse.success(message, application);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取申请", description = "根据ID获取申请信息")
    public ApiResponse<Application> getApplicationById(@PathVariable Long id) {
        log.debug("获取申请请求: ID={}", id);
        Application application = applicationService.getApplicationById(id);
        return ApiResponse.success(application);
    }

    @GetMapping("/user/{userId}")
    @Operation(summary = "获取用户的申请", description = "获取指定用户的申请列表")
    public ApiResponse<IPage<Application>> getApplicationsByUser(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "10") long size) {
        
        log.debug("获取用户申请请求: 用户ID={}", userId);
        Page<Application> page = new Page<>(current, size);
        IPage<Application> applications = applicationService.getApplicationsByUserPage(userId, page);
        return ApiResponse.success(applications);
    }

    @GetMapping("/team/{teamId}")
    @Operation(summary = "获取团队的申请", description = "获取指定团队的申请列表")
    public ApiResponse<IPage<Application>> getApplicationsByTeam(
            @PathVariable Long teamId,
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "10") long size) {
        
        log.debug("获取团队申请请求: 团队ID={}", teamId);
        Page<Application> page = new Page<>(current, size);
        IPage<Application> applications = applicationService.getApplicationsByTeamPage(teamId, page);
        return ApiResponse.success(applications);
    }

    @GetMapping("/team/{teamId}/status/{status}")
    @Operation(summary = "获取团队特定状态的申请", description = "获取指定团队特定状态的申请列表")
    public ApiResponse<IPage<Application>> getApplicationsByTeamAndStatus(
            @PathVariable Long teamId,
            @PathVariable ApplicationStatus status,
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "10") long size) {
        
        log.debug("获取团队特定状态申请请求: 团队ID={}, 状态={}", teamId, status);
        Page<Application> page = new Page<>(current, size);
        IPage<Application> applications = applicationService.getApplicationsByTeamAndStatusPage(teamId, status, page);
        return ApiResponse.success(applications);
    }

    @GetMapping("/team/{teamId}/pending")
    @Operation(summary = "获取团队待处理申请", description = "获取指定团队的待处理申请列表")
    public ApiResponse<List<Application>> getPendingApplicationsByTeam(@PathVariable Long teamId) {
        log.debug("获取团队待处理申请请求: 团队ID={}", teamId);
        List<Application> applications = applicationService.getApplicationsByTeamAndStatus(teamId, ApplicationStatus.PENDING);
        return ApiResponse.success(applications);
    }

    @GetMapping("/check")
    @Operation(summary = "检查申请状态", description = "检查用户是否已申请加入团队")
    public ApiResponse<Boolean> hasApplied(
            @RequestParam Long userId,
            @RequestParam Long teamId) {
        
        log.debug("检查申请状态请求: 用户ID={}, 团队ID={}", userId, teamId);
        boolean hasApplied = applicationService.hasApplied(userId, teamId);
        return ApiResponse.success(hasApplied);
    }
    
    @GetMapping("/user/{userId}/pending")
    @Operation(summary = "获取用户待处理申请", description = "获取指定用户的待处理申请列表")
    public ApiResponse<List<Application>> getPendingApplicationsByUser(@PathVariable Long userId) {
        log.debug("获取用户待处理申请请求: 用户ID={}", userId);
        List<Application> applications = applicationService.getPendingApplicationsByUser(userId);
        return ApiResponse.success(applications);
    }
    
    @GetMapping("/user/{userId}/pending/page")
    @Operation(summary = "分页获取用户待处理申请", description = "分页获取指定用户的待处理申请列表")
    public ApiResponse<IPage<Application>> getPendingApplicationsByUserPage(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "10") long size) {
        
        log.debug("分页获取用户待处理申请请求: 用户ID={}", userId);
        Page<Application> page = new Page<>(current, size);
        IPage<Application> applications = applicationService.getPendingApplicationsByUserPage(userId, page);
        return ApiResponse.success(applications);
    }
    
    @GetMapping("/manager/pending")
    @Operation(summary = "获取管理团队待处理申请", description = "获取当前用户管理的所有团队中的待处理申请列表")
    public ApiResponse<List<Application>> getPendingApplicationsForManager() {
        // 模拟获取当前用户ID
        Long currentUserId = 1L;
        log.debug("获取管理团队待处理申请请求: 管理员ID={}", currentUserId);
        List<Application> applications = applicationService.getPendingApplicationsForManager(currentUserId);
        return ApiResponse.success(applications);
    }
    
    @GetMapping("/manager/pending/page")
    @Operation(summary = "分页获取管理团队待处理申请", description = "分页获取当前用户管理的所有团队中的待处理申请列表")
    public ApiResponse<IPage<Application>> getPendingApplicationsForManagerPage(
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "10") long size) {
        
        // 模拟获取当前用户ID
        Long currentUserId = 1L;
        log.debug("分页获取管理团队待处理申请请求: 管理员ID={}", currentUserId);
        Page<Application> page = new Page<>(current, size);
        IPage<Application> applications = applicationService.getPendingApplicationsForManagerPage(currentUserId, page);
        return ApiResponse.success(applications);
    }
} 